package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.bean.CartInfo;
import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CartAsyncServiceImpl {
    @Resource
    CartInfoMapper cartInfoMapper;
    @Async
    public void updateCartInfoCount(String userKey,String skuId, CartInfo cartInfo) {
        //更新数据库中购物项的数量: 根据userKey+skuId查找购物项修改数量
        //参数1：存入要更新的数据 ，参数2：携带查询数据的条件
        //只需要更新数量
        CartInfo c = new CartInfo();
        c.setCount(cartInfo.getCount());
        int i = 1/0;
        //数据备份
        cartInfoMapper.update( c, Wrappers.lambdaUpdate(CartInfo.class)
                .eq(CartInfo::getUserId , userKey)
                .eq(CartInfo::getSkuId , skuId));
    }
    @Async
    public void saveCartInfo(String userKey,CartInfo cartInfo) {
        //保存购物项到数据库表中
        cartInfoMapper.insert(cartInfo);
    }
    //将未登录的购物项的userId修改为已登录用户的id
    @Async //参数2：未登录购物车用户的userKey
    public void updateCartInfosUserId(String userKey,List<CartInfo> unloginCartInfos){
        // update cart_info set userid = userid  where userid = userKey and skuId=skuid
        // 更新时： 希望更新未登录的购物项 userKey改为 userId
        unloginCartInfos.forEach(unloginCartInfo->{
            cartInfoMapper.update( unloginCartInfo, Wrappers.lambdaUpdate(CartInfo.class)
                    .eq(CartInfo::getSkuId , unloginCartInfo.getSkuId())
                    .eq(CartInfo::getUserId ,userKey));
        });
    }

    //删除未登录购物项  更新已登录购物项的数量
    @Async
    public void deleteAndUpdateCartInfo(String userKey,CartInfo unloginCartInfo,CartInfo loginCartInfo){
        cartInfoMapper.delete(Wrappers.lambdaQuery(CartInfo.class)
            .eq(CartInfo::getSkuId,unloginCartInfo.getSkuId())
            .eq(CartInfo::getUserId , userKey));
        cartInfoMapper.update( loginCartInfo, Wrappers.lambdaUpdate(CartInfo.class)
                .eq(CartInfo::getSkuId , loginCartInfo.getSkuId())
                .eq(CartInfo::getUserId , loginCartInfo.getUserId()));

    }
    @Async
    public void updateCartInfoBySkuIdAndUserKey(String userKey,String skuId,  CartInfo c) {
        // 如果c的属性值为null会被忽略
        c.setId(null);
        cartInfoMapper.update( c, Wrappers.lambdaUpdate(CartInfo.class)
                .eq(CartInfo::getUserId , userKey)
                .eq(CartInfo::getSkuId , skuId));
    }
    @Async
    public void deleteCartInfoBySkuId(String userKey, String skuId) {
        cartInfoMapper.delete(Wrappers.lambdaQuery(CartInfo.class)
            .eq(CartInfo::getUserId , userKey)
            .eq(CartInfo::getSkuId , skuId));
    }
    @Async
    public void batchDelCartInfo(String userKey, List<String> checkedCartInfoSkuIds) {

        cartInfoMapper.delete(Wrappers.lambdaQuery(CartInfo.class)
            .eq(CartInfo::getUserId , userKey)
            .in(CartInfo::getSkuId ,checkedCartInfoSkuIds ));

    }
}
